package com.ltt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ltt.annotation.NoAuth;
import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Back;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.FeedBackParams;
import com.ltt.service.FeedBackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/10 14:59
 */
@CrossOrigin
@Api(tags = "意见反馈模块")
@RestController
@RequestMapping("/feedback")
public class FeedBackController {
    @Autowired
    private FeedBackService feedBackService;

    @ApiOperation("插入反馈意见")
    @PostMapping("/insert")
    public Result insertFeedBack(String content){
        FeedBackParams feedBackParams = new FeedBackParams();
        feedBackParams.setContent(content);
        feedBackParams.setOpenid(UserThreadLocal.get().getOpenid());
        feedBackService.insertFeedBack(feedBackParams);

        return Result.successWithoutData();
    }

    @ApiOperation("获取所有")
    @PostMapping("/get")
    @NoAuth
    public Result getList(@RequestBody String isRead){
        System.out.println(isRead);
        JSONObject jsonObject =  JSON.parseObject(isRead);
        List<Back> backs = feedBackService.getList(Integer.parseInt(jsonObject.getString("isRead")));
        return Result.successWithData(backs);
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    @NoAuth
    public Result del(@RequestBody String id){
        System.out.println(id);
        JSONObject jsonObject =  JSON.parseObject(id);
        feedBackService.delBack(Integer.parseInt(jsonObject.getString("id")));
        return Result.successWithoutData();
    }

    @ApiOperation("查询")
    @PostMapping("/find")
    @NoAuth
    public Result find(@RequestBody String content){
        JSONObject jsonObject =  JSON.parseObject(content);
        System.out.println(content);
        List<Back> backs = feedBackService.find(jsonObject.getString("openId"), Integer.parseInt(jsonObject.getString("isRead")));
        return Result.successWithData(backs);
    }
}
